<template>
  <div class="app-container">
    <el-card>
      <el-form :model="searchForm" inline size="small">
        <el-form-item label="产品名称">
          <el-input clearable v-model="searchForm.keyword" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="查询时间：" prop="beginTime">
          <el-date-picker style="width: 200px" v-model="searchForm.beginTime" align="right" type="date"
            placeholder="选择开始日期" :picker-options="$pickerDateOptions" value-format="yyyy-MM-dd"
            format="yyyy 年 MM 月 dd 日">
          </el-date-picker>
        </el-form-item>
        <el-form-item>-</el-form-item>
        <el-form-item prop="endTime">
          <el-date-picker style="width: 200px" v-model="searchForm.endTime" align="right" type="date"
            placeholder="选择结束日期" :picker-options="$pickerDateOptions" value-format="yyyy-MM-dd"
            format="yyyy 年 MM 月 dd 日">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" @click="handleResetList">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="handleSearchList">查询</el-button>
        </el-form-item>
      </el-form>
      <div>
        <router-link :to="{ path: '/article/addAnswer' }">
          <el-button size="small" icon="el-icon-plus" type="primary">新增问答</el-button>
        </router-link>
        <el-button size="small" icon="el-icon-delete" @click="batchDelete()">批量删除</el-button>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" border style="margin-top: 20px"
        @selection-change="selectTableList" size="mini">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="productName" align="center" label="产品名称"></el-table-column>
        <el-table-column prop="questionRecordCount" align="center" label="问题记录">
          <template slot-scope="scope">
            <el-button type="text" @click="seeQuestion(scope.row)">{{
              scope.row.questionRecordCount
              }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" align="center" label="创建时间"></el-table-column>
        <el-table-column prop="updateTime" align="center" label="更新时间"></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="editAnswer(scope.row)">详情</el-button>
            <el-button size="mini" type="text" @click="editAnswer(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="deleteAnswer(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="pageRight" background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]" :current-page="searchForm.pageNum" :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="tableData.total">
      </el-pagination>
    </el-card>
    <detail :productQstnCate="productQstnCate" ref="detail"></detail>
    <questionDetail :productQstnCate="productQstnCate" ref="questionDetail"></questionDetail>
  </div>
</template>

<script>
import { getAnswerList, deleteAnswerProduct } from "@/api/IntelligentService";
import { getDictData } from "@/utils/dict";
import detail from "./detail.vue";
import questionDetail from "./questionDetail.vue";
import { Loading } from "element-ui";
export default {
  components: { detail, questionDetail },
  data() {
    return {
      searchForm: {
        keyword: "",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10,
      },
      tableData: {
        data: [],
        total: 0,
      },
      searchTime: [],
      listLoading: false,
      selectList: [],
      productQstnCate: [],
      selectList: [],
    };
  },
  async created() {
    this.productQstnCate = await getDictData("product_qstn_cate");
    this.getList();
  },
  methods: {
    selectTableList(val) {
      this.selectList = val.map(v => v.productId)
    },
    seeQuestion(v) {
      this.$refs.questionDetail.openLog(v);
    },
    editAnswer(v) {
      this.$refs.detail.openLog(v);
    },
    deleteAnswer(v) {
      this.$confirm("此操作将永久删除该问答, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteAnswerProduct(v.productId).then((res) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getList();
        });
      });
    },
    handleSearchList() {
      this.searchForm.pageNum = 1;
      this.$authDate(this.searchForm.beginTime, this.searchForm.endTime, v => {
        if (!v.type) {
          return this.$message.warning(v.msg);
        } else {
          // console.log(this.searchForm);
          this.getList();
        }
      });
    },
    handleResetList() {
      this.searchForm = {
        keyword: "",
        startTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10,
      };
      this.searchTime = [];
      this.getList();
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getList();
    },
    batchDelete() {
      if (this.selectList.length === 0) return this.$message.warning("请选择要删除的数据")
      this.$confirm("此操作将永久删除该问答, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteAnswerProduct(this.selectList.join(",")).then((res) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        this.getList();
      })
    },
    getList() {
      this.listLoading = true;
      getAnswerList(this.searchForm)
        .then((response) => {
          this.tableData.data = response.data.list;
          this.tableData.total = response.data.total;
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    },
  },
};
</script>

<style></style>
